﻿using Net5ApiANdWpfClient.Lib.Model;
using Net5ApiANdWpfClient.Lib.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfClient.SystemManger
{
    /// <summary>
    /// PositioninfoUpdate.xaml 的交互逻辑
    /// </summary>
    public partial class PositioninfoUpdate : Window
    {
        Net5ApiANdWpfClient.Lib.Model.SystemAdmin.positioninfo but = new Net5ApiANdWpfClient.Lib.Model.SystemAdmin.positioninfo();
        positioninfoService infoUpdate = new positioninfoService();
        public PositioninfoUpdate()
        {
            InitializeComponent();
            FanInfo();

        }
        public void FanInfo()//反填
        {
            var ids = positioninfo.Ppo_ids;
            but = infoUpdate.positionUpdate(ids);
            this.DataContext = but;
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)//修改
        {
         Net5ApiANdWpfClient.Lib.Model.SystemAdmin.positioninfo list = JsonConvert.DeserializeObject<Net5ApiANdWpfClient.Lib.Model.SystemAdmin.positioninfo>(JsonConvert.SerializeObject(this.DataContext));
          bool Up=  infoUpdate.PositioninfoPut(list);
            if(Up)
            {
                MessageBox.Show("修改成功");
                MainWindow main = null;
                foreach (var w in Application.Current.Windows)
                {
                    if (w.GetType() == typeof(MainWindow))
                    {
                        main = w as MainWindow;
                    }

                }
                // MainWindow main = Application.Current.Windows

                //    FirstOrDefault(c=>c.).Cast<MainWindow>().;
                if (main != null)
                {
                    positioninfo paike = main.MainMdiWindow.Children.FirstOrDefault(c => c.Content.GetType() == typeof(positioninfo)).Content as positioninfo;
                    if (paike != null) paike.Search();
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败");
                this.Close();
            }
            // MessageBox.Show($"{ids}");
        }

        private void fanBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
